package com.qst.dao.impl;

import com.qst.dao.MenuDao;
import com.qst.entity.Menu;
import com.qst.utils.DBHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuMySqlDaoImpl  implements MenuDao {
    @Override
    public List<Menu> getAll() {
        DBHelper dbHelper=new DBHelper();
        dbHelper.setSql("Select * from menu");
        ResultSet resultSet = dbHelper.executeQuery();
        List<Menu> menuList=new ArrayList<>();
        try {
            while (resultSet.next()) {
                Menu menu=new Menu(
                        resultSet.getInt(1),
                        resultSet.getInt(2),
                        resultSet.getString(3),
                        resultSet.getString(4)
                );
                menuList.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return menuList;
    }

    @Override
    public List<Menu> getMenuByRole(int roleID) {
        DBHelper dbHelper=new DBHelper();
        dbHelper.setSql("Select menu.* from menu INNER JOIN " +
                "relation_role_menu r on menu.menuID=r.menuID where r.roleID="+roleID);
        ResultSet resultSet = dbHelper.executeQuery();
        List<Menu> menuList=new ArrayList<>();
        try {
            while (resultSet.next()) {
                Menu menu=new Menu(
                        resultSet.getInt(1),
                        resultSet.getInt(2),
                        resultSet.getString(3),
                        resultSet.getString(4)
                );
                menuList.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return menuList;
    }
}
